package com.example.administrator.atfragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;

import com.example.administrator.atfragment.utils.TextFontUtils;
import com.example.administrator.atfragment.view.VerticalImageSpan;

import java.lang.reflect.Member;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends Activity {

    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.xi_add_status_text);
        editText.setFilters(new InputFilter[]{new MyInputFilter()});
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK || data == null) {
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
        int index = editText.getSelectionStart();
        Editable edit = editText.getEditableText();//获取EditText的文字
        Bundle bundle = data.getExtras();
        Person person = new Person();
        person.setId(bundle.getString(PersonActivity.KEY_CID));
        person.setName(bundle.getString(PersonActivity.KEY_NAME));

        //一次@多个人 循环生成图片时间较慢,待优化
        edit.insert(index, " ");
        edit.insert(index, getBitmapMime(person));
    }

    /**
     * 根据名称生成SpannableString
     *
     * @param person 需要@的人
     * @return
     */
    private SpannableString getBitmapMime(Person person) {
        String mid = getString(R.string.xs_status_add_$s_member, person.getId());
        SpannableString ss = new SpannableString(mid);
        VerticalImageSpan span = new VerticalImageSpan(MainActivity.this, getMembersDrawable("@" + person.getName()));
        ss.setSpan(span, 0, mid.length(), SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
        return ss;
    }


    /**
     * 根据名称生成Drawable图片
     *
     * @param name
     * @return
     */
    private Bitmap getMembersDrawable(String name) {
        Paint paint = new Paint();
        paint.setTextSize(getResources().getDimensionPixelOffset(
                R.dimen.xd_pager_me));
        paint.setAntiAlias(true);
        paint.setColor(getColor(R.color.xc_$FF31C37C));
        Rect rect = new Rect();
        paint.getTextBounds(name, 0, name.length(), rect);
        int width = (int) (paint.measureText(name));
        int height = TextFontUtils.getFontHeight(MainActivity.this.getResources(), R.dimen.xd_pager_me);
        final Bitmap bmp = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        canvas.drawText(name, rect.left, height - rect.bottom, paint);
        return bmp;
    }

    /**
     * 识别输入框的是不是@符号
     */
    public class MyInputFilter implements InputFilter {

        @Override
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            // TODO Auto-generated method stub
            if (source.toString().equalsIgnoreCase("@")
                    || source.toString().equalsIgnoreCase("＠")) {
                goAt();
            }

            return source;
        }

        /**
         * 存储@的cid、name对
         */
        private Map<String, String> cidNameMap = new HashMap<String, String>();

        /**
         * 选中的@的人的cid,进入@列表时，需要传递过去
         */
        private String selectedCids;
        private static final int CODE_PERSON = 1;

        private void goAt() {
            StringBuffer tmp = new StringBuffer();
            // 把选中人的id已空格分隔，拼接成字符串
            for (Map.Entry<String, String> entry : cidNameMap.entrySet()) {
                tmp.append(entry.getKey() + " ");
            }

            Intent intent = new Intent(MainActivity.this, PersonActivity.class);
            intent.putExtra(PersonActivity.KEY_SELECTED, tmp.toString());
            startActivityForResult(intent, CODE_PERSON);
        }
    }


}
